<?php

namespace app\services;

use app\models\user\UserLevel;
use app\services\base\BaseService;
use app\models\user\User;

class Chain
{
    use BaseService;

    public static function getChildren($user_id)
    {
        $list = User::find()
            ->where(['referrer_id' => $user_id])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
            ->with(['level0'])
            ->asArray()
            ->all();

        if (!empty($list)) {
            foreach ($list as &$user) {
                unset($user['password']);
            }
        }

        return $list;
    }

    public static function getChildrenTreeSort($user_id)
    {
        $list = User::find()
            ->where(['referrer_id' => $user_id])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
            ->with(['level0'])
            ->asArray()
            ->all();

        if (!empty($list)) {
            foreach ($list as &$user) {
                unset($user['password']);
            }
        }

        return $list;
    }

    // 有排序 2+1, 必须是有身份的
    public static function getChildrenSort($user_id, $fields = [])
    {
        if ($fields) {
            $list = User::find()
                ->where(['referrer_id' => $user_id])
                ->andWhere(['>=', 'level', UserLevel::$keys['vip1']])
                // ->orderBy(['id' => SORT_ASC])
                ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
                ->select($fields)
                ->column();
        } else {
            $list = User::find()
                ->where(['referrer_id' => $user_id])
                ->andWhere(['>=', 'level', UserLevel::$keys['vip1']])
                // ->orderBy(['id' => SORT_ASC])
                ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
                ->asArray()
                ->all();
        }

        return $list;
    }

    public static function getChildrenLevel($user_id, $level = [])
    {
        if (!empty($level)) {
            $list = User::find()
                ->where(['referrer_id' => $user_id])
                ->andWhere(['level' => $level])
                // ->orderBy(['id' => SORT_ASC])
                ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
                ->with(['level0'])
                ->asArray()
                ->all();
        } else {
            return [];
        }

        if (!empty($list)) {
            foreach ($list as &$user) {
                unset($user['password']);
            }
        }

        return $list;
    }

    // 快的
    public static function getChildrenAllQuick($user_id, array $level, array $fields = [], array $list = [])
    {
        $query = User::find()
            ->where(['referrer_id' => $user_id])
            // ->andWhere(['!=', 'mobile', ''])
            // ->with(['level0'])
            ->andWhere(['level' => $level]);
        if (!empty($fields)) {
            $query->select($fields);
        }
        $children = $query->asArray()
            ->all();

        if (!empty($children)) {
            foreach ($children as $user) {
                array_push($list, $user);
                $list = self::getChildrenAllQuick($user['id'], $level, $fields, $list);
            }
        }

        return $list;
    }

    public static function getChildrenAll($user_id, array $level = [], array $list = [])
    {
        $children = self::getChildren($user_id);

        if (!empty($children)) {
            foreach ($children as $user) {
                if ($level) {
                    if (in_array($user['level'], $level)) {
                        array_push($list, $user);
                    }
                } else {
                    array_push($list, $user);
                }
                $list = self::getChildrenAll($user['id'], $level, $list);
            }
        }

        return $list;
    }

    public static function getReferrer($user_id, $is_object = false)
    {
        $referrer_id = User::find()
            ->where(['id' => $user_id])
            ->select(['referrer_id'])
            ->scalar();

        if ($referrer_id > 0) {
            if ($is_object) {
                return User::find()
                    ->where(['id' => $referrer_id])
                    ->one();
            } else {
                $user = User::find()
                    ->where(['id' => $referrer_id])
                    ->with(['level0'])
                    ->asArray()
                    ->one();
                if ($user) {
                    unset($user['password']);
                    return $user;
                }
            }
        }

        return null;
    }

    public static function getReferrerId($user_id)
    {
        $referrer_id = User::find()
            ->where(['id' => $user_id])
            ->select(['referrer_id'])
            ->scalar();

        if ($referrer_id > 0) {
            return User::find()
                ->where(['id' => $referrer_id])
                ->select(['id', 'level'])
                ->asArray()
                ->one();
        }

        return null;
    }

    public static function getReferrer2($user_id, $is_object = false)
    {
        $referrer_id = User::find()
            ->where(['id' => $user_id])
            ->select(['referrer_id'])
            ->scalar();

        if ($referrer_id > 0) {
            if ($is_object) {
                return User::find()
                    ->where(['id' => $referrer_id])
                    ->one();
            } else {
                $user = User::find()
                    ->where(['id' => $referrer_id])
                    ->with(['level0', 'level2'])
                    ->asArray()
                    ->one();
                if ($user) {
                    unset($user['password']);
                    return $user;
                }
            }
        }

        return null;
    }

    // 新等级链
    public static function getReferrerChainNew($user_id, array $level = [], $is_object = false, array $list = [])
    {
        $user = self::getReferrer($user_id, $is_object);

        if ($user) {
            if ($level) {
                if (in_array($user['new_level'], $level)) {
                    array_push($list, $user);
                }
            } else {
                array_push($list, $user);
            }
            return self::getReferrerChainNew($user['id'], $level, $is_object, $list);
        }

        return $list;
    }

    public static function getReferrerChain($user_id, array $level = [], $is_object = false, array $list = [])
    {
        // $user = self::getReferrer2($user_id, $is_object);
        $user = self::getReferrer($user_id, $is_object);

        if ($user) {
            if ($level) {
                if (in_array($user['level'], $level)) {
                    array_push($list, $user);
                }
            } else {
                array_push($list, $user);
            }
            return self::getReferrerChain($user['id'], $level, $is_object, $list);
        }

        return $list;
    }

    // 只获取1个
    public static function getReferrerChainOne($user_id, array $level = [], $is_object = true)
    {
        $user = self::getReferrer($user_id, $is_object);

        if ($user) {

            if (in_array($user['level'], $level)) {
                return $user;
            } else {
                return self::getReferrerChainOne($user['id'], $level, $is_object);
            }
        }
    }

    public static function getReferrerChainOneId($user_id, array $level = [])
    {
        $user = self::getReferrerId($user_id);

        if ($user) {
            if (in_array($user['level'], $level)) {
                return $user;
            } else {
                return self::getReferrerChainOne($user['id'], $level);
            }
        }
    }

    /**
     * get user list by level
     *
     * @param $level
     * @param false $has_sale Has sale award
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getUsersByLevel($level, $has_sale = false)
    {
        $query = User::find()
            ->where(['level' => $level])
            ->andWhere(['status' => 1]);

        if ($has_sale) {
            $query->andWhere(['reset_sale' => 0]);
        }

        return $query->all();
    }
}